package logic;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import domain.Share;

public class NodeHandler extends ItemHandler {
	
	private HashMap<String, Object> m_KeyValuePairs;
	private ArrayList<Observer> m_Observers;
	private NodeEnum m_HostEnum;
	private int m_Id;
	
	public NodeHandler() {
		m_KeyValuePairs = new HashMap<String, Object>();
		m_Observers = new ArrayList<Observer>();
	}
	
	@Override
	public void startDocument() throws SAXException {
		m_HostEnum = NodeEnum.None;
		m_KeyValuePairs.clear();
	}
	
	@Override
	public void endDocument() throws SAXException {
		notifyObservers(logic.KeyAction.Preferences, m_KeyValuePairs);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		ArrayList<Share> shares;
		Share share;
		
		shares = null;
		
		if (qName.equals("node")) {
			m_HostEnum = NodeEnum.Node;
			m_KeyValuePairs.put(m_Id + "", new HashMap<Object, Object>());
			
			// Add Id and Ip-Address
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("id", m_Id);
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("name", attributes.getValue("name"));
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("username", attributes.getValue("username"));
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("password", attributes.getValue("password"));
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("ipaddress", attributes.getValue("ipaddress"));
			
		} else if (qName.equals("shares")) {
			m_HostEnum = NodeEnum.Shares;
			
			// Add container for shares
			shares = new ArrayList<Share>();
			((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).put("shares", shares);
			
		} else if (qName.equals("share")) {
			shares = (ArrayList<Share>) ((HashMap<String, Object>) m_KeyValuePairs.get(m_Id + "")).get("shares");
			share = new Share();
			share.setName(attributes.getValue("name"));
			share.setUsername(attributes.getValue("username"));
			share.setPassword(attributes.getValue("password"));
			shares.add(share);
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		
		if (qName.equals("node")) {
			m_Id++;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		
		switch (m_HostEnum) {	
			case Node:
			case Nodes:
			case None:
			case Share:
			case Shares:
				break;

			default:
				break;
		}
	}
	
	

	public synchronized void notifyObservers() {
		
		for (Observer o : m_Observers) {
			o.update();
		}
	}

	public void addObserver(Observer observer) {
		m_Observers.add(observer);
		
	}

	public void removeObserver(Observer observer) {
		m_Observers.remove(observer);
	}

	public void notifyObservers(logic.KeyAction keyAction, HashMap<String, Object> keyValuePairs) {
		
		for (Observer o : m_Observers) {
			o.update(keyAction, keyValuePairs);
		}
	}
}
